﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Diagnostics;
using System.Web.Configuration;
using System.Configuration;
using Newtonsoft.Json;
using System.Security.Principal;
using CloudStorageLight.SharePointWeb.Models;
using CloudStorageLight.Core;
using System.IO.Compression;
using System.Threading;
using Microsoft.AspNet.Identity;
using Microsoft.WindowsAzure.Storage.Table;
using CloudStorageLight.Core.Web;

namespace CloudStorageLight.SharePointWeb.Controllers
{
    /// <summary>
    /// FilesControllerのヘルパー
    /// </summary>
    [Authorize]
    public class HelperController : HelperControllerBase
    {
        protected override BlobService CreateBlobService()
        {
            //BasicAuthenticateUtil.Init();
            return new BlobService();
        }


        protected override BlobUser GetCurrentUser()
        {
            if (_user == null)
            {
                _user = BasicAuthenticateUtil.GetCurrentUser();
            }
            return _user;
        }
        private BlobUser _user;


        protected string GetCurrentUserName()
        {
            return System.Web.HttpContext.Current.User.Identity.Name;
        }


        public ActionResult UserRoleList(string term)
        {
            var cloudAccount = BlobService.GetCurrentCloudAccount();
            List<string> items;
            if (cloudAccount.AzureAdMode)
            {
                var authority = cloudAccount.Authority;
                var cloudStorageLightAppKey = cloudAccount.CloudStorageLightAppKey;
                var cloudStorageLightClientId = cloudAccount.CloudStorageLightClientId;
                var tenantId = cloudAccount.TenantId;

                items = AzureAdUtil.ListGroup(authority,cloudStorageLightClientId, cloudStorageLightAppKey, tenantId).Select(x => x.DisplayName)
                    .Concat(AzureAdUtil.ListUser(authority, cloudStorageLightClientId, cloudStorageLightAppKey, tenantId).Where(x => x.UserType == "Member").Select(x => x.UserPrincipalName)).OrderBy(x => x).ToList();
            }
            else
            {
                items = new List<string>();
            }

            var filteredItems = items.Where(
                item => item.IndexOf(term, StringComparison.InvariantCultureIgnoreCase) >= 0
                ).Take(200);
            return Json(filteredItems, JsonRequestBehavior.AllowGet);
        }


        [AllowAnonymous]
        public ActionResult Error(int id = 500)
        {
            Response.StatusCode = id;
            return View();
        }

        [SharePointContextFilter]
        public override Task<ActionResult> DownloadAsZip(FormCollection fc)
        {
            return base.DownloadAsZip(fc);
        }
    }
}